<?php
namespace App\Services\Pay;

use App\Models\Admin\Setting;

class PayWechatconfig
{
	protected $gateWay='https://api.mch.weixin.qq.com/v3';
	protected $appid;
	protected $min_appid;
	protected $min_appsecret;
	protected $app_appid;
	protected $mchid;
	protected $key;
	protected $appsecret ;
	protected $auth_type ='WECHATPAY2-SHA256-RSA2048';
	protected $apiKey3;
	protected $certificateSerialNumber;
	protected $privateKeyPath;
	protected $publicKeys=array();
	protected $privateKey;
	protected $cert;
	protected $minUrl = 'https://api.weixin.qq.com/sns/jscode2session';

	public $status = 0;
	public $description;
	public $out_trade_no;
	public $attach;
	public $notifyUrl;
	public $amount;
	public $transaction_id='';
	public $out_refund_no;
	public $refund=0;
	public $refund_amount;

	public function __construct(){
		global $_G;
        $cache = Setting::getCache();
		$this->notifyUrl = $_G['siteurl'].'api/pay/notify.php';
		$this->appid = $cache['pay_wechat_appid'];
		$this->mchid = $cache['pay_wechat_mchid'];
		$this->status = intval($cache['pay_wechat_status']);
		//$this->privateKeyPath = 'api/pay/apiclient_key.pem';
		$this->privateKey = $cache['pay_wechat_privateKey'];
		$this->certificateSerialNumber=$cache['pay_wechat_certNo'];
		$this->apiKey3 =$cache['pay_wechat_key'];
		$this->appsecret = $cache['pay_wechat_appsecret'];
		$this->cert = 'app/Services/Pay/cert';
		$this->min_appid = $cache['pay_wechat_min_appid'];
		$this->min_appsecret = $cache['pay_wechat_min_appsecret'];
		$this->app_appid = $cache['pay_wechat_app_appid'];
	}

	public function setOutTradeNo($out_trade_no)
	{
		$this->out_trade_no = $out_trade_no;
	}

	public function setOutRefundNo($out_refund_no)
	{
		$this->out_refund_no = $out_refund_no;
	}

	public function setTransactionId($transaction_id)
	{
		$this->transaction_id = $transaction_id;
	}

	public function setRefund($refund)
	{
		$this->refund = $refund;
	}

	public function setDescription($description)
	{
		$this->description = $description;
	}

	public function setAttach($attach)
	{
		$this->attach = $attach;
	}

	public function setNotifyUrl($notifyUrl='')
	{
		$this->notifyUrl = $notifyUrl;
	}

	public function setAmount($amount)
	{
		$this->amount = $amount;
	}

	public function price($num)
	{
		$num = ceil(floatval($num)*100);
		return $num>0?$num:0;
	}

	public function setTotalAmount($totalAmount)
	{
		return $this->amount = $this->price($totalAmount);
	}

	public function setRefundAmount($refund_amount)
	{
		return $this->refund_amount = $this->price($refund_amount);
	}
}
